/*
 * @Description: 
 * @Author: your name
 * @version: 
 * @Date: 2024-01-24 15:29:32
 * @LastEditors: WangShuai 1830773767@qq.com
 * @LastEditTime: 2025-05-17 18:38:38
 */
import * as Cesium from "cesium";
// 工具函数
// 更新3dtiles的模型矩阵
export const update3dtilesMaxtrix = (tx, ty, tile) => {
  const cartographic = Cesium.Cartographic.fromCartesian(
    tile.boundingSphere.center
  );
  const surface = Cesium.Cartesian3.fromRadians(
    cartographic.longitude,
    cartographic.latitude,
    cartographic.height
  );
  const m = Cesium.Transforms.eastNorthUpToFixedFrame(surface);
  const _tx = tx ? tx : 0;
  const _ty = ty ? ty : 0;
  const _tz = 200;
  const tempTranslation = new Cesium.Cartesian3(_tx, _ty, _tz);
  const offset = Cesium.Matrix4.multiplyByPoint(
    m,
    tempTranslation,
    new Cesium.Cartesian3(0, 0, 0)
  );
  const translation = Cesium.Cartesian3.subtract(
    offset,
    surface,
    new Cesium.Cartesian3()
  );
  tile.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
};

// 渲染自定义模型着色器
export const renderLightEffect=(vetrixShader,fragmentShader,tile)=>{
  const radius=tile.boundingSphere.radius
  console.log(tile.boundingSphere)
  const customShader = new Cesium.CustomShader({
    uniforms: {
      u_NightTexture: {
        // 传入图片，需要实例化TextureUniform类，相当于我们webgl中的loadTexture函数
        value: new Cesium.TextureUniform({
          url: "/src/assets/wall.png",
        }),
        // 声明类型，图片为SAMPLER_2D
        type: Cesium.UniformType.SAMPLER_2D,
      },
      u_envTexture:{
        // 传入图片，需要实例化TextureUniform类，相当于我们webgl中的loadTexture函数
        value: new Cesium.TextureUniform({
          url: "/src/assets/pic.jpg",
        }),
        // 声明类型，图片为SAMPLER_2D
        type: Cesium.UniformType.SAMPLER_2D
      },
      maxHeight: {
        type: Cesium.UniformType.FLOAT,
        value: 500.0,
      },
      minHeight: {
        type: Cesium.UniformType.FLOAT,
        value: 0.0,
      },
      // 扫光设置
      u_min:{
        type:Cesium.UniformType.FLOAT,
        value:-radius/3.0
      },
      u_max:{
        type:Cesium.UniformType.FLOAT,
        value:radius/3.0
      },
      u_live_width:{
        type:Cesium.UniformType.FLOAT,
        value:200.0
      }
    },
    varyings: {
      // 声明变量的同时，声明类型
      v_normalMC: Cesium.VaryingType.VEC3,
      v_color:Cesium.VaryingType.VEC3
    },
    // 顶点着色器
    vertexShaderText:vetrixShader,
    // 片元着色器
    fragmentShaderText:fragmentShader
  });
  tile.customShader=customShader
  // 我试出来,模型的最高高度为240，最低高度为120
  const maxHeight = 240.0;
  const minHeight = 120.0;
  tile.customShader.setUniform("maxHeight", maxHeight);
  tile.customShader.setUniform("minHeight", minHeight);
}

// 生成随机点
export const generateRandomPosition = (position, num, radius = 0.05) => {
  let list = [];
  const { lng, lat: latitude } = position;
  for (let i = 0; i < num; i++) {
    // random产生的随机数范围是0-1，需要加上正负模拟
    let lon = lng + Math.random() * radius * (i % 2 == 0 ? 1 : -1);
    let lat = latitude + Math.random() * radius * (i % 2 == 0 ? 1 : -1);
    list.push([lon, lat]);
  }
  return list;
};

//获取一个圆的边缘坐标
export const generateCirclePoints=(center, radius)=> {
	let points = [];
	for (let i = 0; i <= 360; i += 2) {
		points.push(getCirclePoint(center[0], center[1], i, radius));
	}
	return points;
}

export const getCirclePoint=(lon, lat, angle, radius)=> {
	let dx = radius * Math.sin(angle * Math.PI / 180.0);
	let dy = radius * Math.cos(angle * Math.PI / 180.0);
	let ec = 6356725 + (6378137 - 6356725) * (90.0 - lat) / 90.0;
	let ed = ec * Math.cos(lat * Math.PI / 180);
	let newLon = (dx / ed + lon * Math.PI / 180.0) * 180.0 / Math.PI;
	let newLat = (dy / ec + lat * Math.PI / 180.0) * 180.0 / Math.PI;
	return [newLon, newLat];
}
